<?php

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * Coupon
 * @controllerUrl 'coupon'
 * @property int id
 * @property int num
 * @property int price
 * @property string coupon_name
 */
class Coupon extends Model
{
    use SoftDelete;

    // 表名
    protected $name = 'coupon';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    protected $type = [
        'start_time' => 'timestamp:Y-m-d',
        'end_time' => 'timestamp:Y-m-d',
        'delete_time' => 'timestamp:Y-m-d H:i:s',
    ];


    public function getGoodsIdsAttr($value, $row)
    {
        if ($value == '') {
            return [];
        }
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setGoodsIdsAttr($value, $row)
    {
        if ($value && is_array($value)) {
            return implode(',', $value);
        }
        return $value ? $value : '';
    }


    public function getCategoryIdsAttr($value, $row)
    {
        if ($value == '') {
            return [];
        }
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCategoryIdsAttr($value, $row)
    {
        if ($value && is_array($value)) {
            return implode(',', $value);
        }
        return $value ? $value : '';
    }

    public function goods()
    {
        return $this->belongsTo(Goods::class, 'goods_ids', 'id');
    }

    public function category()
    {
        return $this->belongsTo(Category::class, 'category_ids', 'id');
    }
}